import {OfficalState,brandInter,LetterInter} from '../../interface/offical';
import {INIT_BRANDLLIST} from '../types/offical.type';

const initState:OfficalState = {
    brandList:[]
}
 
const officalRducer = (state=initState,action:any) => {
    switch (action.type) {
        case INIT_BRANDLLIST:
            let {list} = action;
            //格式化数据 ['a','b']'
            let keys:string[] = [...new Set(list.map((item:brandInter) => item.Spelling.substr(0,1)))] as string[];

            let brandList:LetterInter[] = keys.reduce((prev:LetterInter[],key:string) => {
                prev = [
                    ...prev,
                    {
                        title:key,
                        list:list.filter((item:brandInter) => item.Spelling.substr(0,1) === key)
                    }
                ]
                return prev
            },[])

            return {
                ...state,
                brandList
            };
    
        default:
            return state;
    }
}

export default officalRducer

